$(function () {
    // 订单倒计时
    intDiff()
    // 加载订单信息
    loadOrder()
})


function intDiff() {
    // 订单倒计时
    let intDiff = parseInt(600);
    setInterval(function () {
        var minute = 0, second = 0;//时间默认值
        if (intDiff > 0) {
            minute = Math.floor(intDiff / 60);
            second = Math.floor(intDiff) - (minute * 60);
        }
        if (minute <= 9) minute = '0' + minute;
        if (second <= 9) second = '0' + second;
        $('.minute_show').html('<s></s>' + minute + ':');
        $('.second_show').html('<s></s>' + second);
        intDiff--;
        if (minute <= 0 && second <= 0) {
            clearInterval(timer); // 超时关闭窗口前先关闭轮询
            timer = 0;
            window.close();
        }
    }, 1000)
}


/**
 * 生成订单并回显支付二维码
 */
let timer

function loadOrder() {
    $.ajax({
        url: "/pay/wxPay",
        type: "post",
        data: {
            "outTradeNo": $('#outTradeNo').text(),  // 商户订单号，必须要唯一
            "actualMoney": parseInt($('#actualMoney').text() * 100), // 实际交易价格
            "totalAmount": $('#actualMoney').text(),
            "paperId": $('#paperId').text()
        },
        dataType: "json",
        success: function (data) {
            var str = data.url;
            $("#qr-code").qrcode({
                width: 180,
                height: 180,
                text: str
            });
        }
    });
    //调用函数定时检查
    //关闭二维码时停止该定时器
    timer = setInterval(startTimer, 3000);
}

/**
 * 开始定时器
 */
function startTimer() {
    $.ajax({
        url: "/pay/queryWxOrder",
        type: "post",
        dataType: "json",
        data: {outTradeNo: $('#outTradeNo').text()},
        success: function (data) {
            if (data.code === '000000') {
                //关闭窗口，跳转到个人中心
                endTimer();
                location.href = "/"; // 后面跳转到个人中心看记录
            } else {

            }
        }
    })
}

/**
 * 关闭定时器
 */
function endTimer() {
    clearInterval(timer);
    timer = 0;
}
